<?

//Data loading
$title='DEBIT NOTE';
$title2='';
if (GetOfficeID()) $perush = GetOfficeLocation (GetOfficeID());
else $perush = 'PT. MASAJI PRAYASA CARGO';

$page   = 'PAGE';
//die();
$DEFINE_FONT=0;
$pdf=new PDFReport("P","mm","Letter");
$pdf->AddPage();
$Result = GridList (GetParam("nobkt",""));
$pdf->GridTable($Result);
$pdf->Output();

function GridList ($TransNo) {
	  $Field = ReadHeader($TransNo);
	  global $DecimalPoint;
	  $DecimalPoint = ($Field->curr<>"IDR")?2:0;
//	  print $DecimalPoint;die();
	  $DBConnection = new mydb(DB_USER, DB_PASSWORD, DB_NAME, DB_HOST);
    if ($Field->inc_ppn==1) 
      $SQL = "SELECT *,jumlah*110/100 as jumlah FROM ais_memorial WHERE nobkt='".$TransNo."' AND urut<>999 AND skada <> ".GetPPNSkada()." ORDER BY urut";
    else	  
    $SQL = "SELECT * FROM ais_memorial WHERE nobkt='".$TransNo."' AND urut<>999 ORDER BY urut";
//	  die($SQL);
    return $DBConnection->dbc->get_results($SQL,ARRAY_A);
}

function ReadHeader($nokwit) {
	global $DBConnection;

    $SQL = "
      SELECT ais_memorial.*,DATE_FORMAT(depdat,'%d %M, %Y') as spell_date, DATE_FORMAT(tglbkt,'%d %M, %Y') as trans_date, SUM(jumlah)  as TOTALPAID, CUADDR, CUPHONE, CUCODE, DATE_FORMAT(duedate,'%d/%m/%Y') as trans_duedate
      FROM ais_memorial
      LEFT JOIN tbldebtur ON ais_memorial.debtur=tbldebtur.cucode
      WHERE nobkt='$nokwit' GROUP BY nobkt 
    ";
    //print $SQL;die();
  return $DBConnection->dbc->get_row($SQL);
}

class PDFReport extends PDF {
	function Header() {
		global $title,$title2,$perush,$page,$DEFINE_FONT,$DBConnection;

		//$this->Ln();$this->Ln();$this->Ln();$this->Ln();$this->Ln();
		if ($DEFINE_FONT==0) {
			$this->AddFont('Arial','','arialn.php');
			$DEFINE_FONT=1;
		}
        				$Field = ReadHeader(GetParam("nobkt",""));

                $this->spell_date = strtoupper($Field->spell_date);
                $this->trans_date = strtoupper($Field->trans_date);
                list($this->year,$this->month,$this->day) = explode("-",$Field->tglbkt);

		        		$this->SetFont('Arial','',12);
                $this->SetLeftMargin(7);
                
                $this->Cell(200,13,"",0,1,'L',0);

                $this->Ln();//$this->Ln();//$this->Ln();$this->Ln();$this->Ln();
                $this->Cell(50,7,"",0,0,'L',0);
                $this->SetFont('Arial','',16);

                $this->Cell(100,7,$title,0,0,'C',0);

                $this->SetFont('Arial','',10);

		        //$this->Ln();
                $this->Cell(200,3,"",0,1,'L',0);		        
                $this->SetLeftMargin(7);

                //$this->Line(5,30,210,30);
                $this->Ln();$this->Ln();$this->Ln();$this->Ln();
                $this->Ln();
                
                //Header Debit Note

                $this->Cell(25,4,"",0,0,'L',0);
                $this->Cell(50,4,$Field->debnam,0,0,'L',0);
                $this->Cell(75,4,"",0,0,'L',0);
                $this->Cell(50,4,GetParam("nobkt",""),0,1,'L',0);
                $this->Cell(25,4,"",0,0,'L',0);
                $this->MultiCell(75,4,trim($Field->CUADDR),0,'L',0);
                $this->SetXY(5,65);
				        $this->Ln();
                $this->Cell(150,7,"",0,0,'L',0);
                $this->Cell(50,7,$Field->curr,0,1,'L',0);
                $this->Cell(45,7,"",0,0,'L',0);
                $this->Cell(105,7,"CODE: ".$Field->debtur,0,0,'L',0);
                $this->Cell(50,5,$Field->trans_duedate,0,1,'L',0);

//		        $this->Ln();
                $this->Cell(10,4,"We Debit Your Account",0,0,'L',0);
                $this->Cell(50,4,"",0,1,'L',0);
		        $this->Ln();
		        $this->Ln();
		        $this->Ln();
		        $this->Ln();
		        $this->Ln();
	}

	function GridTable($MyResult) {
	    //Column widths
	global $DBConnection,$DBName,$DecimalPoint;
	$DBC1 = new mydb(DB_USER, DB_PASSWORD, OfficeDB1, DB_HOST);
	$w=array(5,10,80,30,40,20);
	    //Data
        if ($MyResult)
	    foreach($MyResult as $row)
	    {
            $Total = $Total + $row["jumlah"];
	        $this->Cell($w[0],5,$row["urut"].".",0,0,'R');
	        $this->Cell($w[1],5,"",0,0,'R');
	        $this->Cell($w[2],5,$row["uraian"],0,0,'L');
            $this->Cell($w[3],5,FormatNumber($row["jumlah"],$DecimalPoint),0,0,'R');
            $this->Cell($w[4],5,$row["skada"],0,0,'C');
            $this->Cell($w[5],5,$row["kdact"],0,0,'C');
	        $this->Ln();
	        if (strlen($row["uraian_a"])) {
				$this->Cell($w[0],5,"",0,0,'R');
				$this->Cell($w[1],5,"            ".$row["uraian_a"],0,0,'L');
				$this->Cell($w[2],5,"",0,0,'R');
				$this->Cell($w[3],5,"",0,0,'C');
				$this->Ln();
			}

	        if (strlen($row["uraian_b"])) {
    	        $this->Cell($w[0],5,"",0,0,'R');
    	        $this->Cell($w[1],5,"            ".$row["uraian_b"],0,0,'L');
                $this->Cell($w[2],5,"",0,0,'R');
                $this->Cell($w[3],5,"",0,0,'C');
    	        $this->Ln();
	        }

	        if (strlen($row["uraian_c"])) {
    	        $this->Cell($w[0],5,"",0,0,'R');
    	        $this->Cell($w[1],5,"            ".$row["uraian_c"],0,0,'L');
                $this->Cell($w[2],5,"",0,0,'R');
                $this->Cell($w[3],5,"",0,0,'C');
    	        $this->Ln();
            }

	        if (strlen($row["uraian_d"])) {
    	        $this->Cell($w[0],5,"",0,0,'R');
    	        $this->Cell($w[1],5,"            ".$row["uraian_d"],0,0,'L');
                $this->Cell($w[2],5,"",0,0,'R');
                $this->Cell($w[3],5,"",0,0,'C');
    	        $this->Ln();
	        }
	    }

        $SQL = "SELECT * FROM ais_memorial WHERE nobkt='".GetParam("nobkt","")."' AND urut=999 ";
        $Field = $DBC1->dbc->get_row($SQL);
        
	    $this->Ln();
	    $this->Cell($w[0],5,"999.",0,0,'R');
	    $this->Cell($w[1],5,"",0,0,'R');
	    $this->Cell($w[2],5,"TOTAL: ",0,0,'L');
			if ($Field->curr=="IDR") $Total = round($Total);	    
        $this->Cell($w[3],5,FormatNumber($Total,$DecimalPoint),0,0,'R');
        $this->Cell($w[4],5,$Field->skada,0,0,'C');
        $this->Cell($w[5],5,$Field->kdact,0,0,'C');        
	    $this->Ln();

	    $this->Ln();
        $cc = new intToRupiah;
        $GetCurrency = $DBC1->dbc->get_var("
        SELECT DISTINCT tunam
        FROM tblcurrency, ais_memorial
        WHERE ais_memorial.nobkt='".GetParam("nobkt","")."' AND
              tblcurrency.tusymbol=ais_memorial.curr AND
              ktrasl='".GetOfficeID()."'
        ");
        //$DBConnection->dbc->debug();
/*
        $cc->setTail($GetCurrency);
        $cc->setNumber($Total);
        $SpellNumber = ucfirst(strtoupper(trim($cc->currency)));
*/

	list($number,$decimal) = explode(".",$Total);

	$cc = new numtoword;
	$SpellNumber = strtoupper(trim($cc->en($Total)));

	    $this->Cell($w[0]+$w[1]+$w[2]+$w[3],5,"SAY : ".$SpellNumber." ".$Field->curr,0,0,'L');
	    $this->Ln();
	    $this->Ln();
	    $this->Ln();
	    if (strlen($this->spell_date))
        	$this->Cell(70,5,GetBranchName().", ".$this->spell_date,0,1,'C',0);	    
      else
        	$this->Cell(70,5,GetBranchName().", ".$this->trans_date,0,1,'C',0);
	    $this->Ln();
	    $this->Ln();
	    $this->Ln();
        $SQL = "SELECT * FROM $DBName.tblprofile WHERE ktrasl='".GetOfficeID()."'";
        $Result = $DBConnection->dbc->get_row($SQL);

      $this->Cell(70,5,"(".$Result->ttddn.")",0,1,'C',0);	    //treasury
	    $this->Ln();
	    $this->Ln();
	    $this->Ln();
	    $this->Ln();
	    $this->Ln();
	    $this->Ln();
	    
	    //$this->Cell($w[0]+$w[1]+$w[2]+$w[3],15,$Result->bankdn,0,0,'L');
	    $this->MultiCell(0,5,$Result->bankdn);
	    //Closure line
	}

	function Footer() {
	    //Closure line
	    //$this->Cell(205,0,'','T');
	}

}


?>
